<?php
	require_once($_SERVER["DOCUMENT_ROOT"]."/alerter/modules/base_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/alerter/modules/account.php");

	class AlertAccountMod extends AlertBaseMod {
	
		
		public static function logFailedLogin($name){
			$conn = self::getConnection();
			$sql =
				sprintf(
					"insert into failed_logins (date, user_name) values (utc_timestamp(), '%s')",
					mysql_real_escape_string($name)
				);
			mysql_query($sql, $conn);
		}
	
		public static function getAccount($name) {
			$conn = self::getConnection();
			$sql =
				sprintf(
					"select * from account where name = '%s'",
					mysql_real_escape_string($name)
				);
			$row = mysql_fetch_assoc(mysql_query($sql, $conn)); 
			if (!$row)
				return null;
			return
				new AlertAccount(
                                                $row["id"],
                                                $row["name"],
                                                $row["level"],
                                                $row["validation"],
                                                $row["pid"],
                                                $row["basic_script_access"],
                                                $row["advanced_script_access"],
                                                $row["administrative_script_access"],
                                                $row["secret_script_access"]
				);
		}
                
		public static function getIdAccount($pilotID) {
			$conn = self::getConnection();
			$sql =
				sprintf(
					"select * from account where pid = %d",
					mysql_real_escape_string($pilotID)
				);
			$row = mysql_fetch_assoc(mysql_query($sql, $conn)); 
			if (!$row)
				return null;
			return
				new AlertAccount(
                                                $row["id"],
                                                $row["name"],
                                                $row["level"],
                                                $row["validation"],
                                                $row["pid"],
                                                $row["basic_script_access"],
                                                $row["advanced_script_access"],
                                                $row["administrative_script_access"],
                                                $row["secret_script_access"],
                                                $row["ffm_upload_permission"]
				);
		}
                
		public static function getAfmAccount($pilotId) {
			$conn = self::getConnection();
			$sql =
				sprintf(
					"select * from account where pid = %d",
					$pilotId
				);
			$row = mysql_fetch_assoc(mysql_query($sql, $conn)); 
			if (!$row)
				return null;
			return
				new AlertAccount(
					$row["id"],
                                        $row["name"],
					$row["level"],
					$row["validation"],
                                        $row["pid"],
                                        $row["basic_script_access"],
                                        $row["advanced_script_access"],
                                        $row["administrative_script_access"],
                                        $row["secret_script_access"]
				);
		}
		
		public static function getAccountById($id) {
			$conn = self::getConnection();
			$sql =
				sprintf(
					"select * from alert_users where id = %d",
					$id
				);
			//echo $sql;
			$row = mysql_fetch_assoc(mysql_query($sql, $conn)); 
			if (!$row)
				return null;
			return
				new AlertAccount(
					$row["id"],
					$row["pilot"],
					$row["level"],
					$row["validation"]
				);
		}
                
		public static function getAccountByPid($pid) {
			$conn = self::getConnection();
			$sql =
				sprintf(
					"select * from alert_users where pid = %d",
					$id
				);
			//echo $sql;
			$row = mysql_fetch_assoc(mysql_query($sql, $conn)); 
			if (!$row)
				return null;
			return
				new AlertAccount(
					$row["id"],
					$row["pilot"],
					$row["level"],
					$row["validation"]
				);
		}

		public static function getAccounts(&$pageNumber, &$pageCount) {
			$conn = self::getConnection();

			$sql = "select count(*) as cnt from alert_users";
			$result = mysql_query($sql, $conn);
			$row = mysql_fetch_assoc($result);
			$recordCount = $row["cnt"];
			$pageCount = ceil($recordCount / SettingsMod::PAGE_RECORDS_PER_PAGE);
			if ($pageNumber > $pageCount)
				$pageNumber = $pageCount;


			if ($pageNumber < $pageCount)
				$recordsPerPage = SettingsMod::PAGE_RECORDS_PER_PAGE;
			else {
				$recordsPerPage = $recordCount % SettingsMod::PAGE_RECORDS_PER_PAGE;
				if ($recordsPerPage == 0 && $recordCount > 0)
					$recordsPerPage = SettingsMod::PAGE_RECORDS_PER_PAGE; 
			}
			$sql =
				sprintf(
					"select * from ( " .
						"select * from (" .
							"select * from alert_users " .
							//$where .
							"order by `pilot` asc " .
							"limit 0, %d" .
						") as tmp1 " .
						"order by `pilot` asc " .
						"limit 0, %d " .
					") as tmp2 " .
					"order by `pilot` asc",
					SettingsMod::PAGE_RECORDS_PER_PAGE * $pageNumber,
					$recordsPerPage
				);
			//echo $sql;
			$accounts = array();
			$result = mysql_query($sql, $conn);
			while ($row = mysql_fetch_assoc($result)) {
				$acc =
					new AlertAccount(
						$row["id"],
						$row["pilot"],
						$row["level"],
						$row["validation"]
					);
				$accounts[] = $acc;
			}
			return $accounts;
		}

		public static function addAccount($acc) {
			$conn = self::getConnection();
			$sql = sprintf(
				"insert into alert_users (pilot, level, validation) values ('%s', '%s', '%s')",
				mysql_real_escape_string($acc->getName()),
				mysql_real_escape_string($acc->getLevel()),
				mysql_real_escape_string($acc->getValidation())
			);
			//echo $sql;
			mysql_query($sql, $conn);
		}

		public static function deleteAccount($pilot) {
			$conn = self::getConnection();
			$sql = sprintf(
				"delete from alert_users where pilot= '%s'",
				$pilot
			);
			echo "<script>alert('".$sql."')</script>";
			mysql_query($sql, $conn);
		}

		public static function updateValidation($pilot, $validation) {
			$conn = self::getConnection();
			$sql = sprintf(
				"update alert_users set validation = '%s' where pilot= '%s'",
				mysql_real_escape_string($validation),
				mysql_real_escape_string($pilot)
			);

			mysql_query($sql, $conn);
		}
		
		public static function updateLevel($pilot, $level) {
			$conn = self::getConnection();
			$sql = sprintf(
				"update alert_users set level = '%s' where pilot= '%s'",
				mysql_real_escape_string($level),
				mysql_real_escape_string($pilot)
			);

			mysql_query($sql, $conn);
		}
	}
?>